package org.jeecg.modules.activiti.listener;

import com.alibaba.fastjson.JSON;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;
import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.modules.activiti.entity.ActBusiness;
import org.jeecg.modules.activiti.service.Impl.ActBusinessServiceImpl;
import org.jeecg.modules.zcgl.entity.CustomerBalanceChangesDetail;
import org.jeecg.modules.zcgl.service.ICustomerInfoService;
import org.jeecg.modules.zcgl.service.impl.CustomerInfoServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 客户预存款退费审批监听
 */
@Component
public class CustomerBalanceRefundListener implements ExecutionListener {


    ActBusinessServiceImpl actBusinessService;

    @Autowired
    private ICustomerInfoService customerInfoService;

    public void notify(DelegateExecution execution) throws Exception {
        if (customerInfoService == null) {
            customerInfoService = (CustomerInfoServiceImpl) SpringContextUtils.getBean("customerInfoServiceImpl");
        }
        if (actBusinessService == null) {
            actBusinessService = (ActBusinessServiceImpl) SpringContextUtils.getBean("actBusinessServiceImpl");
        }

        String eventName = execution.getEventName();
        //start
        if (EVENTNAME_START.equals(eventName)) {
        } else if (EVENTNAME_END.equals(eventName)) {
            ActBusiness actBusiness = actBusinessService.getById(execution.getProcessBusinessKey());
            if (actBusiness != null && "zcgl_customer_info".equalsIgnoreCase(actBusiness.getTableName())) {
                CustomerBalanceChangesDetail vo = JSON.parseObject(actBusiness.getData(), CustomerBalanceChangesDetail.class);
                vo.setCompanyId(actBusiness.getCompanyId());
                customerInfoService.balanceRefund(vo);
            }
        }
//        System.out.println("----" + execution.getProcessBusinessKey() + "" + execution.getProcessDefinitionId());
    }
}
